/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.events.BasicEvent;
import com.bourre.events.EventBroadcaster;
import com.bourre.events.EventType;
import com.bourre.events.StringEvent;

import fever.conf.ParserQueue;
import fever.utils.Stringifier;

/**
 * Basic implementation for all configuration parser class.
 * 
 * <p>Events support : 
 * <ul>
 *   <li>{@link #onParserStartEVENT}</li>
 *   <li>{@link #onParserCompleteEVENT}</li>
 * </ul>
 * 
 * @author Romain Ecarnot
 */
class fever.conf.parsers.ConfigurationParser
{
	//-------------------------------------------------------------------------
	// Events definition
	//-------------------------------------------------------------------------
	
	/** Broadcasted when parsing is complete. */
	public static var onParserCompleteEVENT : EventType = new EventType( "onParserComplete" );
	
	/** Broadcasted when parsing starts. */
	public static var onParserStartEVENT : EventType = new EventType( "onParserStart" );
	
	
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _oEB : EventBroadcaster;
	
		
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Starts parsing.
	 * 
	 * <p>Broadcasts {@link #onParserStartEVENT} event type.
	 */
	public function parse( ) : Void
	{
		_fireEventType( onParserStartEVENT );
	}
	
	/**
	 * Adds passed-in {@code listener} for receiving all events.
	 * 
	 * @param listener {@link fever.conf.ParserQueue}
	 */
	public function addListener( listener : ParserQueue ) : Void
	{
		_oEB.addListener( listener );
	}
	
	/**
	 * Removes passed-in {@code listener} for receiving all events.
	 * 
	 * @param listener {@link fever.conf.ParserQueue}
	 */
	public function removeListener( listener : ParserQueue ) : Void
	{
		_oEB.removeListener( listener );
	}
	
	/**
	 * Returns human readable text indicating parsing state.
	 */
	public function getParsingLabel() : String
	{
		return 'parsing configuration';	
	}
	
	/**
	 * Returns string representation.
	 */
	public function toString() : String
	{
		return Stringifier.parse( this );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function ConfigurationParser()
	{
		_oEB = new EventBroadcaster( this );
	}
	
	private function _fireEventType( type : EventType ) : Void
	{
		_oEB.broadcastEvent( new BasicEvent( type, this ) );
	}
	
	private function _fireEvent( event : StringEvent ) : Void
	{
		event.setTarget( this );
		_oEB.broadcastEvent( event );
	}
}